package GUI;
import CTRLayer.*;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class AddCustomer extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField txtID;
	private JTextField txtFName;
	private JTextField txtLName;
	private JTextField txtPhone;
	private JTextField txtAddress;
	private JTextField txtCity;
	private JTextField txtZIP;
	private boolean errorP = false;
	private boolean errorZ = false;
	private Controller ctr;

	/**
	 * Create the frame.
	 */
	public AddCustomer() {
		
		ctr = new Controller();
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 185);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		setTitle("Add customer");
		contentPane.setLayout(null);
	
		JLabel lblFirstName = new JLabel("First name:");
		lblFirstName.setBounds(10, 36, 70, 14);
		contentPane.add(lblFirstName);
		
		JLabel lblLastName = new JLabel("Last name:");
		lblLastName.setBounds(10, 61, 70, 14);
		contentPane.add(lblLastName);
		
		JLabel lblAddress = new JLabel("Address:");
		lblAddress.setBounds(262, 11, 70, 14);
		contentPane.add(lblAddress);
		
		JLabel lblCity = new JLabel("City:");
		lblCity.setBounds(262, 36, 70, 14);
		contentPane.add(lblCity);
		
		JLabel lblZipCode = new JLabel("ZIP code:");
		lblZipCode.setBounds(262, 61, 70, 14);
		contentPane.add(lblZipCode);
		
		JLabel lblPhone = new JLabel("Phone:");
		lblPhone.setBounds(10, 86, 70, 14);
		contentPane.add(lblPhone);
		
		JLabel lblId = new JLabel("ID:");
		lblId.setBounds(10, 11, 70, 14);
		contentPane.add(lblId);
		
		txtID = new JTextField();
		txtID.setBounds(79, 8, 86, 20);
		contentPane.add(txtID);
	
		txtFName = new JTextField();
		txtFName.setBounds(79, 33, 86, 20);
		contentPane.add(txtFName);
		
		txtLName = new JTextField();
		txtLName.setBounds(79, 58, 86, 20);
		contentPane.add(txtLName);
		
		txtPhone = new JTextField();
		txtPhone.setBounds(79, 83, 86, 20);
		contentPane.add(txtPhone);
		
		txtAddress = new JTextField();
		txtAddress.setBounds(342, 8, 86, 20);
		contentPane.add(txtAddress);
		
		txtCity = new JTextField();
		txtCity.setBounds(342, 33, 86, 20);
		contentPane.add(txtCity);
		
		txtZIP = new JTextField();
		txtZIP.setBounds(342, 58, 86, 20);
		contentPane.add(txtZIP);
		
		JButton btnAddCustomer = new JButton("Add customer");
		btnAddCustomer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addCustomerToDB(txtID.getText(), txtLName.getText(), txtFName.getText(), txtPhone.getText(), txtAddress.getText(), txtCity.getText(), txtZIP.getText());
				
			}
		});
		btnAddCustomer.setBounds(313, 111, 118, 23);
		contentPane.add(btnAddCustomer);
		
		JButton btnBackToPrevious = new JButton("Back to previous");
		btnBackToPrevious.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		btnBackToPrevious.setBounds(10, 111, 118, 23);
		contentPane.add(btnBackToPrevious);
	
	
	}
	
	private void addCustomerToDB(String ID, String lName, String fName, String phone, String address, String city, String zip)
	{
		if(ID.isEmpty() || lName.isEmpty() || fName.isEmpty() || phone.isEmpty() || address.isEmpty() || city.isEmpty() || zip.isEmpty())
		{
			JOptionPane.showMessageDialog(null,
				    "All fields have to be filled.",
				    "Warning",
				    JOptionPane.WARNING_MESSAGE);
		}
		else
		{	
			errorP=false;
			try
			{
				Integer.valueOf(phone);
			}
			catch(Exception e)
			{
				JOptionPane.showMessageDialog(null,
						"The phone number should be a number!",
						"Warning",
						JOptionPane.WARNING_MESSAGE);
				errorP=true;
			}

			if(errorP!=true)
			{
				errorZ=false;
				try
				{
					Integer.valueOf(zip);
				}
				catch(Exception e)
				{
					JOptionPane.showMessageDialog(null,
							"The ZIP code should be a number!",
							"Warning",
							JOptionPane.WARNING_MESSAGE);
					errorZ=true;
				}

				if(errorZ!=true)
				{	
					if((ctr.getCustomer(lName)))
					{
						JOptionPane.showMessageDialog(null,
							"Owner with the same name already exists.",
						   	"Warning",
						   	JOptionPane.WARNING_MESSAGE);
					}
					else
					{
						ctr.newCustomer(ID, lName, fName, phone, address, city, zip);
						
						//table at customer window.addElement(/*???*/);
						
						dispose();
					
					}
				}
			}
		}
	}

	
}
